package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GysptWarehouse;
import com.ruoyi.gcgl.service.IGysptWarehouseService;
import com.ruoyi.gcgl.service.IGysptWarehouseitemService;

/**
 * 库存管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-01
 */
@Controller
@RequestMapping("/gcgl/warehouse")
public class GysptWarehouseController extends BaseController
{
    private String prefix = "gcgl/warehouse";

    @Autowired
    private IGysptWarehouseService gysptWarehouseService;
    @Autowired
    private IGysptWarehouseitemService gysptWarehouseitemService;

    @RequiresPermissions("gcgl:warehouse:view")
    @GetMapping()
    public String warehouse()
    {
        return prefix + "/warehouse";
    }

    /**
     * 查询库存管理列表
     */
    @RequiresPermissions("gcgl:warehouse:list")
    @PostMapping("/list")
    @ResponseBody
    public List<GysptWarehouse> list(GysptWarehouse gysptWarehouse)
    {
        gysptWarehouse.setDeleteFlag(1);
        List<GysptWarehouse> list = gysptWarehouseService.selectGysptWarehouseList(gysptWarehouse);
        return list;
    }

    /**
     * 导出库存管理列表
     */
    @RequiresPermissions("gcgl:warehouse:export")
    @Log(title = "库存管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptWarehouse gysptWarehouse)
    {
    	gysptWarehouse.setDeleteFlag(1);
        List<GysptWarehouse> list = gysptWarehouseService.selectGysptWarehouseList(gysptWarehouse);
        ExcelUtil<GysptWarehouse> util = new ExcelUtil<GysptWarehouse>(GysptWarehouse.class);
        return util.exportExcel(list, "库存管理数据");
    }

    /**
     * 新增库存管理
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap){
    	GysptWarehouse gysptWarehouse = new GysptWarehouse();
    	if (0L != parentId){
    		gysptWarehouse = gysptWarehouseService.selectGysptWarehouseById(parentId);
        }else{
        	gysptWarehouse = new GysptWarehouse();
        	gysptWarehouse.setId(0l);
        	gysptWarehouse.setName("主目录");
        }
    	
        mmap.put("gysptWarehouse", gysptWarehouse);
        return prefix + "/add";
    }

    /**
     * 新增保存库存管理
     */
    @RequiresPermissions("gcgl:warehouse:add")
    @Log(title = "库存管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptWarehouse gysptWarehouse)
    {
    	gysptWarehouse.setStatus(0);
    	gysptWarehouse.setDeleteFlag(1);
    	gysptWarehouse.setCreateAt(new Date());
    	gysptWarehouse.setUpdateAt(new Date());
    	gysptWarehouse.setCreateUser(getLoginName());
    	gysptWarehouse.setUpdateUser(getLoginName());
        return toAjax(gysptWarehouseService.insertGysptWarehouse(gysptWarehouse));
    }

    /**
     * 修改库存管理
     */
    @RequiresPermissions("gcgl:warehouse:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptWarehouse gysptWarehouse = gysptWarehouseService.selectGysptWarehouseById(id);
        mmap.put("gysptWarehouse", gysptWarehouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存管理
     */
    @RequiresPermissions("gcgl:warehouse:edit")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptWarehouse gysptWarehouse)
    {
    	gysptWarehouse.setUpdateAt(new Date());
    	gysptWarehouse.setUpdateUser(getLoginName());
        return toAjax(gysptWarehouseService.updateGysptWarehouse(gysptWarehouse));
    }

    /**
     * 删除库存管理
     */
    @RequiresPermissions("gcgl:warehouse:remove")
    @Log(title = "库存管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gysptWarehouseService.deleteGysptWarehouseByIds(ids));
    }
    
    /**
     * 选择部门树
     * 
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectTypeTree/{id}" })
    public String selectTypeTree(@PathVariable("id") Long id, ModelMap mmap){
    	GysptWarehouse gysptWarehouse = gysptWarehouseService.selectGysptWarehouseById(id);
        mmap.put("gysptWarehouse", gysptWarehouse);
        return prefix + "/tree";
    }
    
    /**
     * 加载部门列表树（排除下级）
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(){
    	GysptWarehouse warehouse = new GysptWarehouse();
    	warehouse.setDeleteFlag(1);
        List<Ztree> ztrees = gysptWarehouseService.selectWarehouseTree(warehouse);
        return ztrees;
    }
}